package cn.est.controller;
import cn.est.pojo.MalfunctionOptions;
import cn.est.constants.Constants;
import cn.est.dto.Page;
import cn.est.dto.Result;
import cn.est.service.MalfunctionOptionsService;
import cn.est.utils.EmptyUtils;
import cn.est.utils.ResultUtils;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;

@Controller
@RequestMapping("/client/malfunctionOptions")
public class MalfunctionOptionsController {

    @Resource
    private MalfunctionOptionsService malfunctionOptionsService;

    @RequestMapping("/search")
    @ResponseBody
    public Page<MalfunctionOptions> searchMalfunctionOptions(@RequestParam Map<String,Object> param) {
        Page<MalfunctionOptions> malfunctionOptionsPage = null;
        try {
            Integer pageNo= EmptyUtils.isEmpty(param.get("pageNo"))? Page.DEFAULT_PAGE_NO:Integer.parseInt((String) param.get("pageNo"));
            Integer pageSize= EmptyUtils.isEmpty(param.get("pageSize"))? Page.DEFAULT_PAGE_SIZE:Integer.parseInt((String) param.get("pageSize"));
            malfunctionOptionsPage  = malfunctionOptionsService.queryMalfunctionOptionsPageByMap(param, pageNo, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return malfunctionOptionsPage;
    }

    @RequestMapping("/saveOrUpdateMalfunctionOptions")
    @ResponseBody
    public Result saveOrUpdateMalfunctionOptions(MalfunctionOptions malfunctionOptions){
        Integer flag=0;
        try {
            if(EmptyUtils.isEmpty(malfunctionOptions.getId())){
                flag=malfunctionOptionsService.qdtxAddMalfunctionOptions(malfunctionOptions);
            }else{
                flag=malfunctionOptionsService.qdtxModifyMalfunctionOptions(malfunctionOptions);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag>=1?ResultUtils.returnSuccess():ResultUtils.returnFail();
    }

    @RequestMapping("/getMalfunctionOptionsById")
    @ResponseBody
    public Result getMalfunctionOptionsById(Long id){
        Result result= ResultUtils.returnSuccess();
        MalfunctionOptions malfunctionOptions=null;
        try {
            malfunctionOptions=malfunctionOptionsService.getMalfunctionOptionsById(id);
            result.setData(malfunctionOptions);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @RequestMapping("/batchRemoveMalfunctionOptions")
    @ResponseBody
    public Result batchRemoveMalfunctionOptions(String ids){
        Integer flag=0;
        try {
            flag=malfunctionOptionsService.qdtxBatchDeleteMalfunctionOptions(ids);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }

    @RequestMapping("/removeMalfunctionOptionsById")
    @ResponseBody
    public Result removeMalfunctionOptionsById(Long id){
        Integer flag=0;
        try {
            flag=malfunctionOptionsService.qdtxDeleteMalfunctionOptionsById(id);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultUtils.returnSuccess();
    }
}
